/*
 * This file is part of ELCube.
 *
 * ELCube is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ELCube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with ELCube.  If not, see <https://www.gnu.org/licenses/>.
 */
package cn.nkpro.elcube.components.financial.cards

import cn.nkpro.elcube.annotation.NkNote
import cn.nkpro.elcube.co.NkApplyCSO
import cn.nkpro.elcube.co.NkCustomObjectManager
import cn.nkpro.elcube.docengine.NkAbstractCard
import cn.nkpro.elcube.docengine.model.DocDefIV
import cn.nkpro.elcube.docengine.model.DocHV
import cn.nkpro.elcube.financial.NkAbstractFinanceMethod
import cn.nkpro.elcube.financial.NkFinanceCustomRule
import org.apache.commons.lang3.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.core.annotation.Order
import org.springframework.stereotype.Component

@Order(100010)
@NkNote("还款规则")
@Component("NkCardPaymentRule")
class NkCardPaymentRule extends NkAbstractCard<List<NkFinanceCustomRule>,Def> {

    @Autowired
    NkCustomObjectManager customObjectManager

    @Override
    List<NkFinanceCustomRule> afterGetData(DocHV doc, List<NkFinanceCustomRule> data, DocDefIV defIV, Def d) {
        processProgramDefs(doc, data, d)
        return super.afterGetData(doc, data, defIV, d) as List
    }

    @Override
    List<NkFinanceCustomRule> afterCreate(DocHV doc, DocHV preDoc, List<NkFinanceCustomRule> data, DocDefIV defIV, Def d) {
        processProgramDefs(doc, data, d)
        return super.afterCreate(doc, preDoc, data, defIV, d) as List
    }

    @Override
    List<NkFinanceCustomRule> calculate(DocHV doc, List<NkFinanceCustomRule> data, DocDefIV defIV, Def d, boolean isTrigger, Object options) {
        processProgramDefs(doc, data, d)
        return super.calculate(doc, data, defIV, d, isTrigger, options) as List
    }

    private void processProgramDefs(DocHV doc, List<NkFinanceCustomRule> data, Def d){
        d.programDefs = null

        if(StringUtils.isNotBlank(d.getMtSpEL())) {
            def context = spELManager.createContext(doc)

            String  mt = spELManager.invoke(d.getMtSpEL(), context)
            if(mt){
                def object = customObjectManager.getCustomObject(mt, NkApplyCSO.class)

                if(object instanceof NkAbstractFinanceMethod){
                    d.programDefs = object.programDefs()
                }

                data.removeIf{ item ->
                    !(d.programDefs.find {pDef->pDef.key == item.program})
                }
            }
        }
    }

    static class Def implements Serializable{
        String mtSpEL   // 计算公式
        List<NkAbstractFinanceMethod.ProgramDef> programDefs
    }
}
